<?php
declare (strict_types = 1);

namespace app\middleware;

use app\common\exception\LogicException;
use app\factory\logic\FactorySignLogic;

/**
 * 检查用户是否已经登录
 */
class CheckFactorySign
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $param = [
            'key' => $request->header('key'),
            'sign' => $request->header('sign'),
            'timestamp' => $request->header('timestamp'),
            'body' => file_get_contents('php://input'),
        ];
        $rule = [
            'key' => 'require|max:32',
            'sign' => 'require',
            'timestamp' => 'require|length:10',
        ];
        validate($rule)->check($param);

        $logic = new FactorySignLogic();
        $info = $logic->checkSign($param['key'], $param['timestamp'], $param['sign'], $param['body']);

        $request->factory_info = $info;
        
        return $next($request);
    }
}
